<template>
  <common_print_dialog singlePrintNumber ref="common_print_dialog" businessType="BIN" v-bind="$attrs" v-on="$listeners"
    :tableModel="models" :printUrl="printUrl" :printHandler="printHandler" :previewUrl="previewUrl"
    :previewHandler="previewHandler" :previewMergeUrl="previewMergeUrl" :previewMergeHandler="previewMergeHandler" />
</template>

<script>
import common_print_dialog from './common_print_dialog.vue'
import role_system_mixin from '@/mixin/role_system_mixin';

export default {
  mixins: [role_system_mixin,],
  components: { common_print_dialog },
  data() {
    return {
      printUrl: null,
      previewUrl: null,
      previewMergeUrl: null,
      models: [
        { key: 'binNo', label: 'label_box_code', 'min-width': 140, 'show-overflow-tooltip': true, },
        { key: 'targetWarehouse', label: 'label_destination_warehouse', 'width': 120, 'show-overflow-tooltip': true, },
        { key: 'skuCategoryNum', label: 'label_sku_kind', 'width': 100, 'show-overflow-tooltip': true, },
        { key: 'skuCount', label: 'label_sku_count_sum', 'width': 100, 'show-overflow-tooltip': true, },
      ]
    }
  },
  props: {

    basePrint: false,

  },
  created() {
    if (this.basePrint) {
      this.printUrl = this.$url['print/basePrint']
      this.previewUrl = this.$url['print/basePreview']
      this.previewMergeUrl = null
    } else if (this.isCustomerSystem) {
      this.printUrl = "/oms-api/client/InboundReceivingClientRpc/printBox.json"
      this.previewUrl = "/oms-api/client/InboundReceivingClientRpc/previewBox.json"
      this.previewMergeUrl = "/oms-api/client/InboundReceivingClientRpc/mergeBox.json"
    } else if (this.isOmsSystem) {
      this.printUrl = "/oms-api/receiving/InboundReceivingRpc/printBox.json"
      this.previewUrl = "/oms-api/receiving/InboundReceivingRpc/previewBox.json"
      this.previewMergeUrl = "/oms-api/receiving/InboundReceivingRpc/mergeBox.json"
    } else if (this.isWmsSystem) {
      this.printUrl = "/wms-api/inbound/WmsInboundOverseasInboundOrderRpc/printBox.json"
      this.previewUrl = "/wms-api/inbound/WmsInboundOverseasInboundOrderRpc/previewBox.json"
      this.previewMergeUrl = "/wms-api/inbound/WmsInboundOverseasInboundOrderRpc/mergeBox.json"
    }

  },
  methods: {


    printHandler(data, rows) {
      if (this.basePrint) {
        data.metaDataList = rows.map(row => ({
          metadata: {
            binNo: row.binNo,
            orderNo: row.orderNo,

            printType: data.type,
            targetWarehouse: row.targetWarehouse,
            customerCode: row.customerCode,
            skuCategoryNum: row.skuCategoryNum,
            skuCount: row.skuCount,
            binWeight: row.binWeight,
            binCount: String(row.binCount),
            packageIndex: String(row.boxNumber),
            skuOnlyCodeNumMap: row.skuOnlyCodeNumMap,
            skuTableData: row.items.map(it => {
              return { "skuName": it.skuName, "skuNameEn": it.skuNameEn, "skuOnlyCode": it.skuOnlyCode, "skuCount": it.quantity }
            })
          },
          printNum: row.printNum,
        }))
      } else {
        data.orderList = []
        data.printType = data.type
        _.forOwn(_.groupBy(rows, 'orderId'), (v, k) => {
          data.orderList.push({
            receivingId: k,
            inboundOrderId: k,
            boxList: v.map(it => ({ boxId: it.boxId, printNum: it.printNum }))
          })
        })
      }
      return data
    },
    previewHandler(data, row) {
      if (this.basePrint) {
        data.metadata = JSON.stringify({
          binNo: row.binNo,
          orderNo: row.orderNo,
          printType: data.type,
          targetWarehouse: row.targetWarehouse,
          customerCode: row.customerCode,
          skuCategoryNum: row.skuCategoryNum,
          skuCount: row.skuCount,
          binWeight: row.binWeight,
          binCount: String(row.binCount),
          packageIndex: String(row.boxNumber),
          skuOnlyCodeNumMap: row.skuOnlyCodeNumMap,
          skuTableData: row.items.map(it => {
              return { "skuName": it.skuName, "skuNameEn": it.skuNameEn, "skuOnlyCode": it.skuOnlyCode, "skuCount": it.quantity }
            })
        })
      } else {
        data.boxId = row.boxId
        data.receivingId = row.orderId
        data.inboundOrderId = row.orderId
        data.printType = data.type
        data.printNum = row.printNum
      }


      return data
    },
    previewMergeHandler(data, rows) {
      if (this.basePrint) {
        data.metaDataList = rows.map(row => ({
          metadata: {
            binNo: row.binNo,
            orderNo: row.orderNo,
            printType: data.type,
            targetWarehouse: row.targetWarehouse,
            customerCode: row.customerCode,
            skuCategoryNum: row.skuCategoryNum,
            skuCount: row.skuCount,
            binWeight: row.binWeight,
            binCount: String(row.binCount),
            packageIndex: String(row.boxNumber),
            skuOnlyCodeNumMap: row.skuOnlyCodeNumMap,
            skuTableData: row.items.map(it => {
              return { "skuName": it.skuName, "skuNameEn": it.skuNameEn, "skuOnlyCode": it.skuOnlyCode, "skuCount": it.quantity }
            })
          },
          printNum: row.printNum,
        }))
      } else {
        data.orderList = []
        data.printType = data.type
        _.forOwn(_.groupBy(rows, 'orderId'), (v, k) => {
          data.orderList.push({
            receivingId: k,
            inboundOrderId: k,
            boxList: v.map(it => ({ boxId: it.boxId, printNum: it.printNum }))
          })
        })
      }
      return data
    },


    show(rows) {

      this.$refs.common_print_dialog.show(rows)
    },



  }
}
</script>
